﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Net;
using System.ComponentModel;
using System.IO;

namespace SlideShowDFG2
{
    /// <summary>
    /// Interaction logic for VideoPreviewWindow.xaml
    /// </summary>
    public partial class VideoPreviewWindow : Window
    {
        public VideoPreviewWindow()
        {
            InitializeComponent();
        }

        string _tempFile;

        string _sourceFile;
        FileLocationProtocol _method;

        public string SourceFile
        {
            get { return _sourceFile; }
            set { _sourceFile = value; }
        }

        public FileLocationProtocol Method
        {
            get { return _method; }
            set { _method = value; }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (_method == FileLocationProtocol.FTP)
            {
                WebClient client = WebClientHelper.GetNewInstance();
                client.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler(client_DownloadFileCompleted);
                client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);

                _tempFile = Path.GetTempFileName();

                lblStatus.Content = "Downloading...";

                client.DownloadFileAsync(new Uri(_sourceFile, UriKind.RelativeOrAbsolute), _tempFile);
            }
            else
            {
                mePlayer.Source = new Uri(_sourceFile);
            }
        }

        private void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
        {
            lblStatus.Content = null;
            pbProgress.Value = 0;
            mePlayer.Source = new Uri(_tempFile);
        }

        private void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            pbProgress.Value = (double)e.ProgressPercentage;
            MainWindow.SetTaskBarProgress((int)e.ProgressPercentage, 100);
        }

        private void btnPlayPause_Click(object sender, RoutedEventArgs e)
        {
            if ((string)btnPlayPause.Content == "Play")
            {
                mePlayer.Play();
                btnPlayPause.Content = "Pause";
            }
            else if ((string)btnPlayPause.Content == "Pause")
            {
                mePlayer.Pause();
                btnPlayPause.Content = "Play";
            }
        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            base.Close();
        }

        private void Window_Closing(object sender, CancelEventArgs e)
        {
            mePlayer.Stop();
        }
    }
}
